package com.mybatisplus.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mybatisplus.entity.BaseResultBean;
import com.mybatisplus.entity.Student;
import com.mybatisplus.mapper.StudentMapper;
import com.mybatisplus.service.StudentService;
import com.mybatisplus.vo.DataVO;
import com.mybatisplus.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


/**
 * @author yancheng
 * @date 2023/3/31
 * apiNote
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    //     全部数据展示
    @Override
    public DataVO findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");
        IPage<Student> studentIPage = new Page<>(page, limit);
        IPage<Student> result = studentMapper.selectPage(studentIPage, null);
        List<Student> studentList = result.getRecords();
        dataVO.setCount(result.getTotal());
        dataVO.setData(studentList);
        return dataVO;
    }

    //    id删除
    @Override
    public void deleteUserByIds(int ids) {
        studentMapper.deleteById(ids);
    }

    //    修改分数
    @Override
    public void updateScoreById(int id, int score) {
        UpdateWrapper<Student> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("score", score);
        studentMapper.update(null, updateWrapper);
    }


    //echarts异步数据加载
    @Override
    public List<Student> SelectScore() {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name", "score");
        List<Student> studentList = studentMapper.selectList(queryWrapper);
        studentList.forEach(System.out::println);
        return studentList;
    }

    //名字查询
    @Override
    public StudentVO selectName(String name) {
        StudentVO studentVO = new StudentVO();
        studentVO.setCode(0);
        studentVO.setMsg("");
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        List<Student> studentList = studentMapper.selectList(queryWrapper);
        studentVO.setData(studentList);
        studentVO.setCount(studentList.size());
        return studentVO;
    }


    //随机选人
    @Override
    public StudentVO choose(int random) {
        StudentVO studentVO = new StudentVO();
        studentVO.setCode(0);
        studentVO.setMsg("");
        List<Object> list = new ArrayList<>();
//        此处为上线使用的代码
        Random r = new Random();
        int temp = 0;
        try {
            for (int i = 0; i < random; i++) {
                int num = r.nextInt(88);
                if (num == temp)
                    num = r.nextInt(88);
                temp = num;
                list.add(studentMapper.selectById(num));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        studentVO.setCount(list.size());
        studentVO.setData(list);
        return studentVO;
    }

    @Override
    public int insertToTable(Student student) {
        System.out.println(student.toString());
        studentMapper.insertFromExcel(student);
        return 0;
    }

    @Override
    public String selectNameByStuid(String stuid) {
        return studentMapper.selectNameByStuid(stuid);
    }

    @Override
    public Student selectStudentByStuid(String stuid) {
        return studentMapper.selectAllByStuid(stuid);
    }

    @Override
    //如果表格存在某个学生的数据，将通过学号更新其分数
    public void updateScoreByStuid(Integer score, String stuid) {
        studentMapper.updateScoreByStuid(score, stuid);
    }


    @Override
    public Student selectStudentById(String stuid) {
        return studentMapper.selectStudentById(stuid);
    }

    @Override
    public BaseResultBean alterPassword(String stuid, String newPassword, HttpServletRequest request) {
        BaseResultBean resultBean = new BaseResultBean();
        studentMapper.updatePasswordByStuid(stuid, newPassword);
        request.getSession().invalidate();
        resultBean.setMessage("密码更改成功，请使用新密码登录");
        resultBean.setStatus(true);
        return resultBean;
    }

}
